package com.haozi.ehub.core.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.haozi.ehub.core.mapper.TestInfoMapper;
import com.haozi.ehub.core.service.TestInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.haozi.ehub.core.util.RedisUtils;
import com.haozi.ehub.entity.code.DataCode;
import com.haozi.ehub.entity.entities.TestInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 测试表 服务实现类
 * </p>
 *
 * @author terry
 * @since 2020-05-29
 */
@Service
@DS(value = DataCode.SLAVE_1)
@SuppressWarnings(value = "all")
public class TestInfoServiceImpl extends ServiceImpl<TestInfoMapper, TestInfo> implements TestInfoService {

    @Autowired
    private RedisUtils redisUtil;

    @Override
    public List<TestInfo> getAll() {
        return baseMapper.selectAll();
    }

    @Override
    public TestInfo getById(String id) {
        TestInfo testInfo = baseMapper.selectById(id);
        redisUtil.set(testInfo.getTestId(), JSONObject.toJSONString(testInfo));
        return testInfo;
    }

    @Override
    public TestInfo getByIdFromCache(String id) {
        String testInfoJson = (String)redisUtil.get(id);
        return JSONObject.parseObject(testInfoJson,TestInfo.class);
    }

    @Override
    public TestInfo getCacheById(String id) {
        return (TestInfo) redisUtil.get(id);
    }
}
